﻿$(document).ready(function(){
    subNav();
    $('.headCtripNav').find('#nav').addClass('cur');

    $('.indexSlider').slide({ titCell: '.nav ul', mainCell: '.pics ul', effect: 'fold',  autoPlay: true, autoPage: true}); //幻灯片
    $('.itemBlock').slide({titCell: '.blockTab li',mainCell: '.blockContent',trigger: 'click',delayTime: 0,switchLoad:"data-original"}); //模块Tab切换
    $('.citySelect').slide({titCell: '.cityTab li',mainCell: '.cityContent',trigger: 'click',delayTime: 0,switchLoad:"data-original"}); //模块Tab切换
    $('.itemBlock .item').slide({titCell: '.subTab li',mainCell: '.subContent .container',trigger: 'click',delayTime: 0,switchLoad:"data-original"}); //模块Tab切换

    $(".searchAll").slide({titCell:".hd li", mainCell: ".bd", autoPlay: false,trigger: "click"}); //搜索框切换

    $('.conditions li.drop .inputText').click(function (event) { //搜索下拉
        event.stopPropagation(); //取消事件冒泡
        $('.conditions li.drop dl').hide();
        $(this).parent().parent().next('dl').show();
    });
    $('.drop dl dd').click(function () {//下拉框操作
        var text = $(this).html();
        var val = $(this).attr('val');
        $(this).siblings().removeClass('on');
        $(this).addClass('on');
        $(this).parent().hide();
        $(this).parent().parent().find('.inputText').val(text);
        $(this).parent().parent().find("input[type='hidden']").val(val);
    });
    $(document).click(function (event) {  //点击空白处或者自身隐藏下拉层
        $('.topCity').removeClass('on');
        $('.conditions li.drop dl').hide();
    });
    $('.searchList li.drop').each(function () {
        $(this).find('dd:first').click();
    });

    //酒店搜索日历绑定
    $('[name="tm1"]').each(function () {
        var today = new Date(Date.parse(webconfig().date.replace(/-/g, '/')));
        today = Date.parse(today.getFullYear() + '/' + (today.getMonth() + 1) + '/' + today.getDate());
        var t2 = '#tm2' + $(this).attr('id').replace('tm1', ''), d, d1;
        var l1 = parseInt($(this).attr('l')), l2 = parseInt($(t2).attr('l'));
        if (!l1) l1 = 1;
        if (!l2) l2 = 1;
        var minadvance = webconfig().hotel.minadvance, maxadvance = webconfig().hotel.maxadvance;
        var mindate, maxdate;
        mindate = today + 86400000 * minadvance;
        maxdate = mindate + 86400000 * maxadvance;
        var tmp = $(this).attr('mindate'), tmp1 = $(this).attr('maxdate');
        if (tmp == undefined) tmp = '2000-1-1';
        if (tmp1 == undefined) tmp1 = '2100-1-1';
        tmp = Date.parse(tmp.replace(/-/g, '/'));
        tmp1 = Date.parse(tmp1.replace(/-/g, '/'));
        if (mindate < tmp) mindate = tmp;
        if (maxdate > tmp1) maxdate = tmp1;
        $(this).datepicker({ minDate: new Date(mindate), maxDate: new Date(maxdate), numberOfMonths: l1, onSelect: function (dateText, inst) {
            var t2 = $(this).parent().find('[name="tm2"]');
            var d = Date.parse(dateText.replace(/-/g, '/'));
            var maxbooking = webconfig().hotel.maxbooking;
            maxbooking = (maxbooking == undefined ? 30 : parseInt(maxbooking));
            t2.datepicker('option', 'minDate', new Date(d + 86400000)).datepicker('option', 'maxDate', new Date(d + 86400000 * maxbooking));
            setTimeout('$(\'#tm2' + $(this).attr('id').replace('tm1', '') + '\').datepicker("show")', 10);
        }
        });
        var maxbooking = webconfig().hotel.maxbooking;
        mindate = Date.parse($(this).val().replace(/-/g, '/')) + 86400000;
        maxdate = mindate + 86400000 * (maxbooking - 1);
        tmp1 += 86400000;
        if (maxdate > tmp1) maxdate = tmp1;
        $(t2).datepicker({ minDate: new Date(mindate), maxDate: new Date(maxdate), numberOfMonths: l2, onClose: function (dateText, inst) {
            if ($(this).attr('fu'))
                setTimeout('eval(\'' + $(this).attr('fu') + '\');', 10);
        }
        });
    });
    //签证搜索
    $("#search_countryname").keyup(function (e) {
        if (!e) var e = window.event;
        var n = e.which || e.keyCode;
        if (n == 40 || n == 48 || n == 13) return;
        showmenu();
    }).keydown(function (e) {
        if (!e) var e = window.event;
        var n = e.which || e.keyCode, s, h;
        switch (n) {
            case 40:
                s = -1,
                h = $("#countrymenu li:gt(0):visible");
                if (h.length < 2) return;
                for (var i = 0; i < h.length; i++) if ($(h[i]).hasClass('selected')) { s = i; break; }
                s = s == -1 || h.length == s + 1 ? 0 : s + 1;
                h.removeClass("selected");
                $(h[s]).addClass('selected');
                break;
            case 38:
                s = 0,
                h = $("#countrymenu li:gt(0):visible");
                if (h.length < 2) return;
                for (var i = 0; i < h.length; i++) if ($(h[i]).hasClass('selected')) { s = i; break; }
                s = s == 0 ? h.length - 1 : s - 1;
                h.removeClass("selected");
                $(h[s]).addClass('selected');
                break;
            case 13:
                h = $('#countrymenu .selected');
                if (h.length == 0) return;
                $(this).val(h.attr('na'));
                $('#vgid').val(h.attr('v'));
                $('#countrymenu').hide();
                break;
            default:
                break;
        }
    }).focus(function () {
        if ($('#continentmenu').length == 0) {
            var a = $(this).offset();
            $(this).after('<ul id="countrymenu" ></ul><div id="continentmenu">加载中...</div>');
            $('#countrymenu,#continentmenu').css({ 'position': 'absolute', 'z-index': 9999 }).offset({ top: a.top + 36, left: a.left });
            $('#countrymenu').hide();
            $.get(webpath + 'sys/xml/visacountry.xml', { t: Math.random() }, function (gxml) {
                var t = '<div class="citytitle"><span title="关闭" onclick="$(\'#continentmenu\').hide();">关闭</span>国家/地区</div><ul class="citytpye">', t1 = '', t2 = '<li class="full">输入中文/英文或上下选择</li>';
                $.get(webpath + 'sys/xml/visacontinent.xml', { t: Math.random() }, function (zxml) {
                    $(zxml).find('row').each(function (i) {
                        var a = $(this).attr('t_id');
                        t += '<li onclick="qh(this,\'selected\',\'\',\'continent\');" id="qh_continent_' + a + '"' + (i == 0 ? '  class="selected"' : '') + '>' + $(this).find('t_name').text() + '</li>';
                        t1 += '<ul class="citys" id="qh_container_continent_' + a + '"' + (i != 0 ? ' style="display:none;"' : '') + '>';
                        $(gxml).find('row[d_pid="' + a + '"]').each(function (i) {
                            var a = $(this).attr('d_cityid'), b = $(this).find('d_name').text(), c = $(this).find('d_english').text();
                            t1 += '<li v="' + a + '">' + b + '</li>';
                            t2 += '<li en="' + c.toLowerCase() + '" na="' + b + '" v="' + a + '"><span>' + c + '</span>' + b + '</li>';
                        });
                        t1 += '</ul>';
                    });
                    $('#continentmenu').html(t + '</ul>' + t1).find('li[v]').click(function () {
                        $("#search_countryname").val($(this).text());
                        $('#vgid').val($(this).attr('v'));
                        $('#continentmenu').hide();
                    });
                    $('#countrymenu').html(t2);
                    $('#countrymenu li:gt(0)').mouseover(function () {
                        $('#countrymenu li:gt(0)').removeClass('selected');
                        $(this).addClass("selected");
                    }).click(function () {
                        $("#search_countryname").val($(this).attr('na'));
                        $('#vgid').val($(this).attr('v'));
                        $('#countrymenu').hide();
                    });
                    showmenu();
                });
            });
            return;
        }
        showmenu();
    }).blur(function () {
        $('#countrymenu').hide('slow')
    });

    //租车日历绑定
    $('[name="cm1"]').each(function () {
        var today = new Date(Date.parse(webconfig().date.replace(/-/g, '/')));
        today = Date.parse(today.getFullYear() + '/' + (today.getMonth() + 1) + '/' + today.getDate());
        var t2 = '#cm2' + $(this).attr('id').replace('cm1', ''), d, d1;
        var l1 = parseInt($(this).attr('l'));
        if (!l1) l1 = 1;
        var minadvance = webconfig().car.minadvance, maxadvance = webconfig().car.maxadvance;
        var mindate, maxdate;
        mindate = today + 86400000 * minadvance;
        maxdate = mindate + 86400000 * maxadvance;
        var tmp = $(this).attr('mindate'), tmp1 = $(this).attr('maxdate');
        if (tmp == undefined) tmp = '2000-1-1';
        if (tmp1 == undefined) tmp1 = '2100-1-1';
        tmp = Date.parse(tmp.replace(/-/g, '/'));
        tmp1 = Date.parse(tmp1.replace(/-/g, '/'));
        if (mindate < tmp) mindate = tmp;
        if (maxdate > tmp1) maxdate = tmp1;
        if ($(t2).length == 0) {
            $(this).datepicker({ minDate: new Date(mindate), maxDate: new Date(maxdate), numberOfMonths: l1 });
        } else {
            var l2 = parseInt($(t2).attr('l'));
            if (!l2) l2 = 1;
            $(this).datepicker({ minDate: new Date(mindate), maxDate: new Date(maxdate), numberOfMonths: l1, onSelect: function (dateText, inst) {
                var t2 = $(this).parent().find('[name="cm2"]');
                var d = Date.parse(dateText.replace(/-/g, '/'));
                var maxbooking = t2.attr('maxbooking');
                maxbooking = (maxbooking == undefined ? 30 : parseInt(maxbooking));
                t2.datepicker('option', 'minDate', new Date(d + 86400000)).datepicker('option', 'maxDate', new Date(d + 86400000 * maxbooking));
                setTimeout('$(\'#cm2' + $(this).attr('id').replace('cm1', '') + '\').datepicker("show")', 10);
            }
            });
            var maxbooking = webconfig().car.maxbooking;
            mindate = Date.parse($(this).val().replace(/-/g, '/')) + 86400000;
            maxdate = mindate + 86400000 * (maxbooking - 1);
            tmp1 += 86400000;
            if (maxdate > tmp1) maxdate = tmp1;
            $(t2).datepicker({ minDate: new Date(mindate), maxDate: new Date(maxdate), numberOfMonths: l2, onClose: function (dateText, inst) {
                if ($(this).attr('fu'))
                    setTimeout('eval(\'' + $(this).attr('fu') + '\');', 10);
            }
            });
        }

    });

    $('#search_visa').click(function () {
        var a = $("#search_countryname").val().toLowerCase();
        var h = $('#countrymenu li[en="' + a + '"],[na="' + a + '"]');
        if (h.length == 0) h = $('#countrymenu li[en^="' + a + '"],[na^="' + a + '"]');
        if (h.length != 0) {
            h = $(h.get(0));
            $("#search_countryname").val(h.attr('na'));
            $('#vgid').val(h.attr('v'));
        }
        if ($('#vgid').val() == 0) { alert('您尚未选择签证国家!'); return; } Search(this, 'vcid,vgid,vtid');
    });

    $('.viewHistory .toogleBar').click(function() {
        if($(this).find('i').hasClass('hidden')){
            $(this).find('i').removeClass('hidden');
            $('.viewHistory .item').slideDown();
        }else{
            $(this).find('i').addClass('hidden');
            $('.viewHistory .item').slideUp();
        }
    });

    $('.othersNav li').click(function(event) {
        var name = $(this).attr('name'),
        url = $(this).attr('url');
        $('#othersMore').html("更多"+ name +" >").attr('href', url);
    });

    $('#flightpanel > li > a').click(function () { //机票Tab切换
        if ($(this).attr('ajaxload') != undefined) return;
        $(this).attr('ajaxload', 1);
        flightData($(this));
    }).first().click();

    //友情链接
    $('.friend').each(function() {
        var find_dd = $(this).find('dd');
        if (find_dd.height() > 30) {
            find_dd.css('height', 22);
            find_dd.append('<span class="btn_links">更多<i class="icon_arrow"></i></span>')
        }
    });
    $('.btn_links').on('click',function() {
        if ($(this).hasClass('links_up')) {
            $(this).removeClass('links_up').html('更多<i class="icon_arrow"></i>').parent().css('height', 22)
        } else {
            $(this).addClass('links_up').html('收起<i class="icon_arrow"></i>').parent().css('height', 'auto')
        }
    });

    //当滚动定位
    $(window).scroll(function(){ 
        var scrollTop = $(window).scrollTop();
        if(scrollTop){
            $('.itemBlock').each(function(i){
                $(this).attr('t', i);
                if(scrollTop >= $(this).offset().top-70){
                    var myT = $(this).attr('t');
                    $('.leftNavBox li').removeClass('on');
                    $('.leftNav li[v="'+myT+'"]').addClass('on');
                }
            });
            if(scrollTop > $('.line').offset().top-100 && scrollTop < $('.travels').offset().top+100){
                $(".leftNav").fadeIn(100);
            }else{
                $(".leftNav").fadeOut(100); 
            }
        }
    });
    $(".leftNav li").each(function(i){
        $(this).attr('v', i);
    }).click(function() { 
        var i = $(this).attr('v'); //取得按钮值
        var t = $('.itemBlock[t="'+ i +'"]'); //对应的id层
        $(".leftNav li").removeClass('on');
        $(this).addClass('on');
        $("html,body").animate({scrollTop: t.offset().top-68}, 250); //250毫秒跳转到该ID的位置
    });
    //当滚动定位结束
});
//二级导航
function subNav(){
    $('.headCtripNav').find('li').hover(
        function(){
            if($(this).hasClass('active')){
                $(this).addClass('up');
                $(this).find('.subnav').show().stop().css('height', '41px');
                
                var subL = $(this).position().left; //一级菜单相对父级的距离
                var subW = $(this).find('.list').width();//二级菜单的宽度
                var navWidth = $(this).width();//当前菜单的宽度
                var w = subL + navWidth/2 - subW/2;
                var wc = subW + w - 1200;
                if( wc > 0){
                    $(this).find('.list').css("padding-left",w - wc - 10);
                }else{
                    $(this).find('.list').css("padding-left",w);
                }
            }
        },
        function(){
            if($(this).hasClass('active')){
                $(this).removeClass('up');
                $(this).find('.subnav').hide().stop().css('height', '0');
            }
        }
    );
};
function showmenu() {
    var o = $('#search_countryname'), a = o.val().toLowerCase(), b = $('#vgid').val();
    if (a == '国家/地区') { o.val(''); $('#vgid').val(0); a = ''; }
    if (a == '') {
        $('#countrymenu').hide();
        $('#continentmenu').show();
    } else {
        $('#continentmenu').hide();
        $('#countrymenu').show();
        $("#countrymenu li").removeClass('selected').filter(':gt(0)').hide().filter('[en^="' + a + '"],[na^="' + a + '"]').show().filter(':visible:first').addClass('selected');
    }
}
//首页机票加载
function flightData(obj, f, df, t, dt) {
    if (!webconfig().flight) return;
    var s=$('#flightpanel > li').index(obj.parent());
    var toDate = new Date(Date.parse(webconfig().date.replace(/-/g, '/')) + 86400000);
    toDate=toDate.getFullYear() + "-" + (toDate.getMonth() + 1) + "-" + toDate.getDate()
    var parma;
    parma = 'fromCity=' + obj.attr('v');
    parma += '&fromDate=' + webconfig().date;
    parma += '&toDate=' + toDate;
    parma += '&rand=' + Math.random();
    $.ajax({
        type: "get",
        url: webpath + "sys/ajax/flight/flight.ashx?act=bind",
        data: parma,
        dataType: "json",
        success: function (data, textStatus) {
            var ul = $('#flightpanel').parent().next('.contentBox').find('.container').children('ul').eq(s);
            ul.empty();
            if (data.status == 1) {
                $.each(data.flights, function (i, d) {
                    var tmp = '<li>';
                    tmp+='      <a target="_blank" href="'+$('#flighturl').val()+'?fromCity='+d.deptcode+'"&toCity='+d.arrcode+'&fromDate='+d.depdate2+'">';
                    tmp+='          <div class="flightCon">';
                    tmp+='              <p class="tit">'+ d.depairport +' - '+ d.arrairport +'</p>';
                    tmp+='              <p class="info"><span class="date">'+d.depdate2+'</span></p>';
                    tmp+='              <p class="info"><span class="price">&yen;<em>'+ d.ticketprice +'</em>起</span><span class="button">立抢</span></p>';
                    tmp+='          </div>';
                    tmp+='      </a>';
                    tmp+='</li>';
                    ul.append(tmp);
                });
            } else {
                ul.append("<li>抱歉，没有搜索到相关的航班信息！</li>");
            }
        }
    });
}